package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.entity.SysRights;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SysRightsMapper {

    @Delete({
        "delete from sys_rights",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Delete({
        "delete from sys_role_rights",
        "where ROLE_ID=#{roleId}"
    })
    int deleteRightsByRoleId(Long roleId);

    @Insert({
        "insert into sys_rights (NAME, SORT_ORDER, ",
        "STATUS, P_ID, REMARK, ",
        "TYPE, LOG_INFO, URL)",
        "values (#{name,jdbcType=VARCHAR}, #{sortOrder,jdbcType=INTEGER}, ",
        "#{status,jdbcType=INTEGER}, #{pId,jdbcType=BIGINT}, #{remark,jdbcType=VARCHAR}, ",
        "#{type,jdbcType=TINYINT}, #{logInfo,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(SysRights record);

    @Insert({
        "insert into sys_role_rights(ROLE_ID,RIGHT_ID)",
        "values(#{roleId},#{rightId})"
    })
    int insertRoleRight(@Param("roleId") long roleId, @Param("rightId") long rightId);

    @InsertProvider(type = SysRightsSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(SysRights record);

    @Select({
        "select",
        "ID, NAME, SORT_ORDER, STATUS, P_ID, REMARK, TYPE, LOG_INFO, URL",
        "from sys_rights",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column = "ID", property = "id", jdbcType = JdbcType.BIGINT, id = true)
        ,
        @Result(column = "NAME", property = "name", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "SORT_ORDER", property = "sortOrder", jdbcType = JdbcType.INTEGER)
        ,
        @Result(column = "STATUS", property = "status", jdbcType = JdbcType.INTEGER)
        ,
        @Result(column = "P_ID", property = "pId", jdbcType = JdbcType.BIGINT)
        ,
        @Result(column = "REMARK", property = "remark", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "TYPE", property = "type", jdbcType = JdbcType.TINYINT)
        ,
        @Result(column = "LOG_INFO", property = "logInfo", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "URL", property = "url", jdbcType = JdbcType.VARCHAR)
    })
    SysRights selectByPrimaryKey(Long id);

    @Select({
        "SELECT r.* FROM sys_rights r",
        "INNER JOIN sys_role_rights rr ON r.ID=rr.RIGHT_ID",
        "INNER JOIN sys_role sr ON rr.ROLE_ID=sr.ID",
        "WHERE sr.ID=#{roleId}"
    })
    List<SysRights> selectByRoleId(long roleId);

    @Select({
        "SELECT r.* FROM sys_rights r",
        "INNER JOIN sys_role_rights rr ON r.ID=rr.RIGHT_ID",
        "INNER JOIN sys_role ro ON ro.ID=rr.ROLE_ID",
        "WHERE r.STATUS=0 AND r.P_ID=#{parentId}",
        "AND ro.ID=#{roleId}",
        "ORDER BY r.SORT_ORDER"
    })
    List<SysRights> selectByRoleIdAndParentId(@Param("roleId") long roleId, @Param("parentId") long parentId);

    /**
     * 根据用户ID和父ID查找权限<br>
     *
     * @param uid 用户ID
     * @param pid 父权限ID
     * @return
     */
    @Select({
        "SELECT r.*",
        "FROM sys_rights r",
        "WHERE r.STATUS=0 AND r.ID IN (",
        "SELECT rr.RIGHT_ID FROM sys_role_rights rr WHERE rr.ROLE_ID IN (",
        "SELECT ur.ROLE_ID FROM sys_user_role ur WHERE ur.USER_ID = #{uid,javaType=long}",
        ")",
        ") AND r.P_ID = #{pid,javaType=long}",
        "ORDER BY r.SORT_ORDER"
    })
    List<SysRights> selectByUserIdAndParentId(@Param("uid") long uid, @Param("pid") long pid);

    /**
     * 根据用户ID查找权限<br>
     *
     * @param uid
     * @return
     */
    @Select({
        "SELECT r.*",
        "FROM sys_rights r",
        "WHERE r.STATUS=0 AND r.ID IN (",
        "SELECT rr.RIGHT_ID FROM sys_role_rights rr WHERE rr.ROLE_ID IN (",
        "SELECT ur.ROLE_ID FROM sys_user_role ur WHERE ur.USER_ID = #{uid,javaType=long}",
        ")",
        ")ORDER BY r.SORT_ORDER"
    })
    List<SysRights> selectByUserId(long uid);

    @UpdateProvider(type = SysRightsSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SysRights record);

    @Update({
        "update sys_rights",
        "set NAME = #{name,jdbcType=VARCHAR},",
        "SORT_ORDER = #{sortOrder,jdbcType=INTEGER},",
        "STATUS = #{status,jdbcType=INTEGER},",
        "P_ID = #{pId,jdbcType=BIGINT},",
        "REMARK = #{remark,jdbcType=VARCHAR},",
        "TYPE = #{type,jdbcType=TINYINT},",
        "LOG_INFO = #{logInfo,jdbcType=VARCHAR},",
        "URL = #{url,jdbcType=VARCHAR}",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(SysRights record);
}
